package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;

import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.TradeHistoryBean;
import com.xmb.wechat.delegate.TradeHistoryDetailDelegate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/22 17:53
 */
public class WechatFundTradeHistoryDetailActivity extends BaseActivity {

    private TradeHistoryDetailDelegate mDelegate;

    public WechatFundTradeHistoryDetailActivity() {
        super(R.layout.activity_fund_trade_history_detail);
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, WechatFundTradeHistoryDetailActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void initView() {
        setStatusBarColor(Color.parseColor("#F2F2F2"));
        setNavigationColor(Color.parseColor("#f7f7f7"));

        mDelegate = new TradeHistoryDetailDelegate(this);
        //数据
        List<TradeHistoryBean> arrayList = WechatStartApplication.getBoxStore().boxFor(TradeHistoryBean.class).query().build().find();
        if (arrayList != null && arrayList.size() > 0)
            sortData(arrayList);
    }

    @SuppressLint("SimpleDateFormat")
    private void sortData(List<TradeHistoryBean> data) {
        //最终的数据合集
        ArrayList<TradeHistoryBean> result = new ArrayList<>();
        //记录时间数据在集合中的角标
        ArrayList<Integer> positions = new ArrayList<>();
        //记录收支情况
        ArrayList<String> expendAndIncomes = new ArrayList<>();
        //排序
        Collections.sort(data);
        //分组
        double expend = 0;//支出
        double inCome = 0;//收入
        TradeHistoryBean pre = null;
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy年M月");
        for (TradeHistoryBean entity : data) {
            if (!isInSameMonth(monthFormat, pre, entity)) {//不是同一月
                //新建一个时间数据
                TradeHistoryBean tradeHistoryEntity = new TradeHistoryBean();
                tradeHistoryEntity.setMonthInYear(monthFormat.format(new Date(entity.getTradeTime())));
                result.add(tradeHistoryEntity);
                //记录新增的时间数据的角标
                positions.add(result.size() - 1);
                //添加上月的收支汇总（第一条时间数据不添加)
                if (pre != null)
                    expendAndIncomes.add("支出 ¥" + expend + " 收入 ¥" + inCome);
                //清空上月的收支汇总
                expend = 0;
                inCome = 0;
            }
            //支出、收入汇总
            if (entity.isExpend())
                expend += entity.getMoney();
            else
                inCome += entity.getMoney();
            //添加交易数据
            result.add(entity);
            pre = entity;
        }
        //添加上月的收支汇总（最后一条数据）
        expendAndIncomes.add("支出 ¥" + expend + " 收入 ¥" + inCome);
        //设置收支情况
        for (int i = 0; i < positions.size(); i++) {
            Integer position = positions.get(i);
            if (result.size() > position && expendAndIncomes.size() > i) {
                TradeHistoryBean tradeHistoryEntity = result.get(position);
                tradeHistoryEntity.setTotal(expendAndIncomes.get(i));
            }
        }
        mDelegate.addData(result);
    }

    //比较两个时间是否一样(年月)
    @SuppressLint("SimpleDateFormat")
    private boolean isInSameMonth(SimpleDateFormat monthFormat, TradeHistoryBean pre, TradeHistoryBean tradeHistoryEntity) {
        if (pre == null)
            return false;
        return TextUtils.equals(monthFormat.format(new Date(pre.getTradeTime())), monthFormat.format(new Date(tradeHistoryEntity.getTradeTime())));
    }

    @OnClick(R2.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
